package cn.com.darly.videoandroidpm.ui.conversation.video;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.darly.common.ToastApp;
import com.darly.dview.common.SCfg;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.SharePreferHelp;
import cn.com.darly.videoandroidpm.common.StaticListener;
import cn.com.darly.videoandroidpm.ui.biz.ConnectListener;
import cn.com.darly.videoandroidpm.ui.biz.ConnectListener.ConnectInterface;
import cn.com.darly.videoandroidpm.ui.presenter.ConnectPresenter;

/**
 * 连接后，双方都进入这个页面，在这个页面进行视频信息展示。
 *
 * @author Darly/张宇辉/2018/5/30 10:19
 * @version 1.0/cn.com.darly.videoandroidpm.ui.conversation.video
 */
@ContentBinder(R.layout.activity_video_connect)
public class VideoConnectionActivity extends BaseActivity implements View.OnClickListener ,ConnectInterface,StaticListener.OnCallConsoleListener {

    @ViewsBinder(R.id.id_connect_stop)
    Button id_connect_stop;
    @ViewsBinder(R.id.id_connect_publish)
    FrameLayout id_connect_publish;

    private LoginInfo friend;//好友信息

    private LoginInfo user;//自己的信息

    private String ownRTMP;//这个是自己的流媒体地址

    private String fwnRTMP;//这个是好友的流媒体地址


    private VideoPublishFragment videoPublishFragment;

    private VideoPlayFragment videoPlayFragment;

    private ConnectPresenter presenter;

    @Override
    protected void initParamer() {
        super.initParamer();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        friend = (LoginInfo) getIntent().getSerializableExtra("LoginInfo");
        if (friend == null) {
            ToastApp.showToast("好友信息为空");
            finish();
        }
        ownRTMP = getIntent().getStringExtra("OWN");
        if (TextUtils.isEmpty(ownRTMP)) {
            ToastApp.showToast("用户流媒体地址错误");
            finish();
        }
        fwnRTMP = getIntent().getStringExtra("FWN");
        if (TextUtils.isEmpty(fwnRTMP)) {
            ToastApp.showToast("好友流媒体地址错误");
            finish();
        }
        user = (LoginInfo) SharePreferHelp.getValue("LoginInfo");
        if (user == null) {
            ToastApp.showToast("用户信息错误");
            finish();
        }
        presenter = new ConnectPresenter(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(SCfg.getWidth()/4,SCfg.getHeight()/4);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        id_connect_publish.setLayoutParams(lp);
    }

    @Override
    protected void loadData() {
        swichTo(true);//左上角录制，播放好友在中间
    }

    @Override
    protected void initListener() {
        id_connect_stop.setOnClickListener(this);
        StaticListener.setOnCallConsoleListener(this);
    }

    public void swichTo(boolean tras) {
        fragmentManager = getSupportFragmentManager();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (tras) {
            if (videoPlayFragment != null) {
                if (videoPlayFragment.isVisible())
                    return;
                ft.show(videoPlayFragment);
            } else {
                videoPlayFragment = new VideoPlayFragment();
                Bundle bundle = new Bundle();
                bundle.putString("fwnRTMP",fwnRTMP);
                videoPlayFragment.setArguments(bundle);
                ft.add(R.id.id_connect_play, videoPlayFragment, TAGSS);
            }
            if (videoPublishFragment != null) {
                if (videoPublishFragment.isVisible())
                    return;
                ft.show(videoPublishFragment);
            } else {
                videoPublishFragment = new VideoPublishFragment();
                Bundle bundle = new Bundle();
                bundle.putString("ownRTMP",ownRTMP);
                videoPlayFragment.setArguments(bundle);
                ft.add(R.id.id_connect_publish, videoPublishFragment, TAGSS);
            }

        } else {
            if (videoPlayFragment != null) {
                if (videoPlayFragment.isVisible())
                    return;
                ft.show(videoPlayFragment);
            } else {
                videoPlayFragment = new VideoPlayFragment();
                Bundle bundle = new Bundle();
                bundle.putString("fwnRTMP",fwnRTMP);
                videoPlayFragment.setArguments(bundle);
                ft.add(R.id.id_connect_publish, videoPlayFragment, TAGSS);
            }
            if (videoPublishFragment != null) {
                if (videoPublishFragment.isVisible())
                    return;
                ft.show(videoPublishFragment);
            } else {
                videoPublishFragment = new VideoPublishFragment();
                Bundle bundle = new Bundle();
                bundle.putString("ownRTMP",ownRTMP);
                videoPlayFragment.setArguments(bundle);
                ft.add(R.id.id_connect_play, videoPublishFragment, TAGSS);
            }
        }
        ft.commit();
    }


    public String getOwnRTMP() {
        return ownRTMP;
    }

    public String getFwnRTMP() {
        return fwnRTMP;
    }

    @Override
    public void onClick(View v) {
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("friendid", friend.getId());
        presenter.onStopConnect(params);

    }

    @Override
    public void onConnectSuccess(String info) {
        if (videoPlayFragment != null) {
            videoPlayFragment.onDestroy();
        }
        if (videoPublishFragment != null) {
            videoPublishFragment.onDestroy();
        }
        finish();
    }

    @Override
    public void onConnectFailed(String info) {

    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }

    @Override
    public void callConsole(String key) {
        //接收到好友断开会话。在这里进行连接断开
        if (videoPlayFragment != null) {
            videoPlayFragment.onDestroy();
        }
        if (videoPublishFragment != null) {
            videoPublishFragment.onDestroy();
        }
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
